package com.atguigu.spzx.pay.service.impl;

import com.atguigu.spzx.feign.order.OrderFeignClient;
import com.atguigu.spzx.model.entity.order.OrderInfo;
import com.atguigu.spzx.model.entity.order.OrderItem;
import com.atguigu.spzx.model.entity.pay.PaymentInfo;
import com.atguigu.spzx.pay.mapper.PaymentInfoMapper;
import com.atguigu.spzx.pay.service.PaymentInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * ClassName: PaymentInfoServiceImpl
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/25 17:28
 * Version 1.0
 */
@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {
    @Resource
    private PaymentInfoMapper paymentInfoMapper;
    @Resource
    private OrderFeignClient orderFeignClient;
    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        //1 根据orderNo查询支付记录表  支付记录是否存在
        PaymentInfo paymentInfo = paymentInfoMapper.getByOrderNo(orderNo);
        //2 支付记录不存在，远程调用：根据订单编号获取订单信息，完成添加
        if(paymentInfo == null){
            //远程调用获取订单信息
            OrderInfo orderInfo = orderFeignClient.getOrderInfoByOrderNo(orderNo);

            //封装到paymentinfo中  最后加到数据库
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPayType(orderInfo.getPayType());
            String content = "";
            for(OrderItem item : orderInfo.getOrderItemList()) {
                content += item.getSkuName() + " ";
            }
            paymentInfo.setContent(content);
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setOrderNo(orderNo);
            paymentInfo.setPaymentStatus(0);
            //添加
            paymentInfoMapper.save(paymentInfo);

        }
        return paymentInfo;
    }
}
